<?php
namespace Controller;
use Controller\Controller;
use Model\UserModel;
use Framework\Verify;
class UserController extends Controller
{

	public $user;

	public function __construct()
	{
		parent::__construct();
		$this->user = new UserModel();

	}
	/*
	public function index()
	{
		//echo '用户你的index方法';
		
		$this->assign('username' , '张三');
		$this->assign('sex' , '张四');
		$this->display();
	}
	*/

	//显示出来注册的模板
	public function register()
	{
		//index是唯一入口文件
		//路径传值
		//var_dump($GLOBALS['config']['WEB_SITE']);
		$public = $GLOBALS['config']['WEB_SITE'].'public/';
		$controller = $GLOBALS['config']['WEB_SITE'].'app/Controller/';
		$vendor = $GLOBALS['config']['WEB_SITE'].'vendor/dj/framework/src/';

		$this->assign('public' , $public);
		$this->assign('controller' , $controller);
		$this->assign('vendor' , $vendor);
		$this->display();
	}

	//执行注册方法
	public function doRegister()
	{
		$data = [];

		//获取注册post信息
		var_dump($_POST);
		$data['username'] = trim($_POST['username']);
		$data['password'] = trim($_POST['password']);
		$repassword = trim($_POST['repassword']);
		$data['email'] = trim($_POST['email']);
		$data['sex'] = $_POST['sex'];
		$data['ctime'] = time();
		$data['birthday'] = strtotime($_POST['birthday_year'].'-'.$_POST['birthday_month'].'-'.$_POST['birthday_day']);
	//	var_dump(date('Y-m-d' , 926697600 ));
		$verify = trim($_POST['verify']);

		//判断各值是否符合标准
		//判断用户名长度
		//var_dump($data['username']);
		if (strlen($data['username']) > 12 || strlen($data['username'])< 3) {
			exit('用户名长度须为 3 ~ 12 个字符,或 1 ~ 4 个汉字');
		}
		//判断用户名是否已存在
		//$username = trim($_POST['username']);
		//$res = $this->user->where("username='$username'")->select();
		$res = $this->user->doFind($data);
		//var_dump($res);
		if ($res) {
			exit('该用户名已存在');
		}


		//判断密码长度
		if (strlen($data['password']) > 12 || strlen($data['password']) < 3) {
			exit('密码长度须为 3 ~ 12 个字符 ');
		}
		//判断密码是否为纯数字
		if (is_numeric($data['password'])) {
			exit('密码不得为纯数字');
		}
		//判断两次密码是否一致
		if ($data['password'] !== $repassword) {
			exit('两次密码输入不一致');
		}
		//密码 md5加密
		$data['password'] = md5($data['password']);


		//判断邮箱格式是否正确
		$ePattern = "/([a-z0-9]*[-_.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?/i";
		if (!preg_match($ePattern,$data['email'])) {
			exit('邮箱格式错误');
		}


		//判断验证码
		var_dump($_SESSION);
		if ($verify !== strtolower($_SESSION['verify'])) {
			exit('验证码输入错误');
		}


		//插入数据库
		$result = $this->user->doAdd($data);

		if ($result) {	
			//echo 111111111111;
			$this->success('注册成功');
		} else {

			//echo 2222222222222;
			$this->error('注册失败');
		}
	}

	//登录的方法
	public function login()
	{
		$view = $GLOBALS['config']['WEB_SITE'].'app/View/';
		$vendor = $GLOBALS['config']['WEB_SITE'].'vendor/dj/framework/src/';
		//var_dump($vendor);
		
		$this->assign('view' , $view);
		$this->assign('vendor' , $vendor);

		$this->display();
	}

	//执行登录
	public function doLogin()
	{
		//接收post的值
		var_dump($_POST);
		$data['username'] = trim($_POST['username']);
		$data['password'] = trim($_POST['password']);
		$verify = trim($_POST['verify']);

		//判断用户名是否为空
		if (empty($data['username'])) {
			exit('用户名不得为空');
		}

		//查询数据库,用户个人信息
		$result = $this->user->doFind($data);
		//var_dump($result);
		
		//判断用户名是否存在
		if (!$result) {

			exit('该用户名尚未注册,无法登录');
		}

		//判断密码是否正确
		$password = $result[0]['password'];
		//var_dump(md5($data['password']));
		//var_dump($password);
	
		if (md5($data['password']) !== $password) {
			exit('密码输入错误');
		}

		//判断验证码是否正确
		if ($verify !== strtolower($_SESSION['verify'])) {
			exit('验证码输入错误');
		}

		$this->success('登录成功');

		//$this->error('登录失败');
		header('')
		//$username = trim($_POST['username']);
		//$res = $this->user->where("username='$username'")->select();
		//var_dump($res);
		
	}

	public function 
}